#include <iostream>
#include <unordered_map>
#include <cstring>
#define int long long
#define endl '\n'
using namespace std;
inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while('0'<=ch&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
inline void write(int x){
    if(x==0){putchar('0');return;}
    if(x<0)putchar('-'),x=-x;
    int buf[1005],len(0);
    while(x)buf[len++]=x%10,x/=10;
    while(len)putchar(buf[--len]+'0');
}
int t,nn,anss;
int a[200005],pre[200005],ad[200005];
unordered_map<int,int> m;
void dfs(int k,int sum,int lastb,int lastr){
    if(k==nn+1){
        anss=max(anss,sum);
        return ;
    }
    if(sum+ad[nn]-ad[k-1]<anss)return ;
    if(a[lastb]==a[k])dfs(k+1,sum+a[k],k,lastr);
    else dfs(k+1,sum,k,lastr);
    if(a[lastr]==a[k])dfs(k+1,sum+a[k],lastb,k);
    else dfs(k+1,sum,lastb,k);
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    t=read();
    while(t--){
        anss=0;
        memset(a,0,sizeof a);memset(pre,0,sizeof pre);memset(ad,0,sizeof ad);
        nn=read();
        for(int i=1;i<=nn;i++){
            a[i]=read();
            pre[i]=m[a[i]];
            m[a[i]]=i;
            ad[i]=ad[i-1]+a[i];
        }
        m.clear(); unordered_map<int,int>().swap(m);
        dfs(1,0,0,0);
        write(anss);putchar('\n');
    }
    return 0;
}
